package com.zdx.yswl.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import com.loopj.android.image.SmartImageView;
import com.zdx.yswl.R;
import com.zdx.yswl.tool.KNetTool;
import com.zdx.yswl.ui.bean.HeadImgBean;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Toast;

/**
 * 适配器
 * 
 * @author Administrator
 */

public class MainPagerAdapter extends PagerAdapter {

	public interface OnClickPagerListener {
		public void onClikPager(View v);
	};

	private OnClickPagerListener mClickListener;
	private List<View> mViews = new ArrayList<View>();
	private List<HeadImgBean> mData = new ArrayList<HeadImgBean>();

	private Context context;

	public MainPagerAdapter(final Context context, List<HeadImgBean> list) {
		super();
		this.context = context;
		this.mData = list;
//		if (mViews == null && mViews.size() <= 0) {
//			mViews = new ArrayList<View>();
//		}
		// 默认图片
		for (int i = 0; i < list.size(); i++) {
			View view = LayoutInflater.from(context).inflate(R.layout.viewpager_item, null);
			SmartImageView image = (SmartImageView) view.findViewById(R.id.image);
			image.setImageResource(R.drawable.aio_image_default_round);
			mViews.add(view);
		}
	}

	public void setData(List<HeadImgBean> mData) {
		this.mData.clear();
		this.mViews.clear();
		
		this.mData = mData;
		List<View> views = new ArrayList<View>();
		LayoutInflater lyInflater = LayoutInflater.from(context);
		// 默认图片
		for (int i = 0; i < mData.size(); i++) {
			View view = lyInflater.inflate(R.layout.viewpager_item, null);
			SmartImageView image = (SmartImageView) view.findViewById(R.id.image);
			image.setImageResource(R.drawable.aio_image_default_round);
			views.add(view);
		}
		mViews = views;
	}

	@Override
	public int getCount() {
//		if (mData != null && mData.size() > 0) {  
//            return mData.size();  
//        } else {  
//            return 0;  
//        }  
		return Integer.MAX_VALUE;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView(mViews.get(position % mData.size()));
		// position % urls.length 取余数
	}

	@Override
	public Object instantiateItem(ViewGroup container, final int position) {
		int num = container.getChildCount();
		for (int i = 0; i < num; i++) {
			if (container.getChildAt(i).hashCode() == mViews.get(position % mData.size()).hashCode()) {
				container.removeView(mViews.get(position % mData.size()));
				break;
			}
		}
		View view = mViews.get(position % mData.size());
		SmartImageView image = (SmartImageView) view.findViewById(R.id.image);
		if (KNetTool.isNetworkAvalible(context)) {
			image.setImageUrl(mData.get(position % mData.size()).url);
		}
//		view.setTag(position % mData.size());
//		view.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				if (mClickListener != null) {
//					mClickListener.onClikPager(v);
//				}
//			}
//		});

		container.addView(view);
		return view;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE ;
	}
	
	public void setAdaperOnClickListener(OnClickPagerListener listener) {
		this.mClickListener = listener;
	}
}